package backtracking.leetcode.medium;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bruin_du
 * @description 二叉树中和为某一值的路径
 * @date 2022/5/28 10:24
 **/
public class Offer34_PathSum {
    List<List<Integer>> list = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        recursion(root,target);
        return list;
    }

    private void recursion(TreeNode root, int target) {
        if(root == null) return;
        path.add(root.val);
        target -= root.val;
        if(target == 0 && root.left == null && root.right == null )
            list.add(new ArrayList<>(path));
        recursion(root.left,target);
        recursion(root.right,target);
        path.remove(path.size() - 1);
    }
}
